package srx.open.com.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.VerifySMSCodeListener;
import de.greenrobot.event.EventBus;
import srx.open.com.R;
import srx.open.com.bean.User;
import srx.open.com.utils.CommonUtils;
import srx.open.com.utils.Log;
import srx.open.com.utils.OpenToast;

/**
 * 注册界面. 修复地方：需要提前修改手机验证.
 *
 * @author hailongqiu 356752238@qq.com
 */
public class RegisterActivity extends ExtendBaseActivity {

    private static final String TAG = RegisterActivity.class.getSimpleName();

    @Bind(R.id.left_tv)
    TextView left_tv;
    @Bind(R.id.right_lay)
    View right_lay;

    @Bind(R.id.one_tv)
    TextView one_tv;
    @Bind(R.id.two_tv)
    TextView two_tv;
    @Bind(R.id.three_tv)
    TextView three_tv;

    @Bind(R.id.register_switch_lay)
    LinearLayout register_switch_lay; // 界面切换.
    /* 手机发送短信界面 */
    @Bind(R.id.register_one_lay)
    LinearLayout register_one_lay; // 手机号码短信发送界面.
    @Bind(R.id.ver_code_btn)
    Button ver_code_btn; // 获取验证码.
    @Bind(R.id.user_et)
    EditText user_et; // 手机号码.
    /* 短信验证界面 */
    @Bind(R.id.register_two_lay)
    LinearLayout register_two_lay; // 短信验证界面.
    //	@ViewById
    @Bind(R.id.ver_code_et)
    EditText ver_code_et; // 验证码
    @Bind(R.id.ok_ver_code_btn)
    Button ok_ver_code_btn; // 验证验证码.
    /* 密码界面(真正的注册结尾) */
    @Bind(R.id.register_three_lay)
    LinearLayout register_three_lay; // 密码界面.
    @Bind(R.id.pass_et)
    EditText pass_et; // 密码
    @Bind(R.id.pass_rept_et)
    EditText pass_rept_et; // 再次确认密码
    @Bind(R.id.ok_register_btn)
    Button ok_register_btn; // 注册.
    /* 法律条文 */
    @Bind(R.id.article_lay)
    LinearLayout article_lay;
    @Bind(R.id.article_chk)
    CheckBox article_chk; // 法律条文

    String password = ""; // 保存密码.

    @Override
    public void initAllDatas() {
    }

    @Override
    public void initAllViews() {
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initTitle();
        initWidgetsEvents();
    }

    /*
     * 标题栏初始化.
     */
    private void initTitle() {
        left_tv.setText("注册");
        right_lay.setVisibility(View.GONE);
    }

    private void initWidgetsEvents() {
        article_chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                articleChkCheckedChange(isChecked);
            }
        });
        pass_rept_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                passReptEtAfterTextChange(s);
            }
        });
        //
        pass_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                passEtAfterTextChange(s);
            }
        });
        //
        ver_code_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                verCodeEtAfterTextChange(s);
            }
        });
        //
        user_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                userEtAfterTextChange(s);
            }
        });
    }

    /*
     * 获取验证码.
     */
    @OnClick(R.id.ver_code_btn)
    void ver_code_btn(View view) {
        phoneStr = user_et.getText().toString();
        // 查找手机是否已被注册.
        checkUserName(phoneStr, "提示", "此手机号码已被注册，验证短信可登陆", "发送验证码");
    }

    @Override
    public void onCheckUserNoexist() {
        // 使用Bmob的发送短信.
        BmobSMS.requestSMSCode(mContext, phoneStr, "forgetPass", new RequestSMSCodeListener() {
            @Override
            public void done(Integer integer, BmobException ex) {
                if (ex == null) {
                    OpenToast.showMsg("验证码已发送");
                    // 切换到短信验证界面.
                    one_tv.setTextColor(mResource.getColor(R.color.black));
                    two_tv.setTextColor(mResource.getColor(R.color.head_bg));
                    // 切换短信验证界面.
                    register_one_lay.setVisibility(View.GONE);
                    ver_code_btn.setVisibility(View.GONE);
                    article_lay.setVisibility(View.GONE);
                    register_two_lay.setVisibility(View.VISIBLE);
                    ok_ver_code_btn.setVisibility(View.VISIBLE);
                } else {
                    OpenToast.showMsg("验证码发送失败");
                }
            }
        });
    }

    /*
     * 标题栏返回按钮.
     */
    @OnClick(R.id.left_lay)
    void left_lay(View view) {
        finish();
    }

    /*
     * 验证验证码.
     */
    @OnClick(R.id.ok_ver_code_btn)
    void ok_ver_code_btn(View view) {
        String verCode = ver_code_et.getText().toString();
        BmobSMS.verifySmsCode(mContext, phoneStr, verCode, new VerifySMSCodeListener() {
            @Override
            public void done(BmobException ex) {
                if (null == ex) {
                    Log.d("提交验证码成功");
                    // 开始切换到密码的注册界面.
                    // 颜色切换.
                    two_tv.setTextColor(mResource.getColor(R.color.black));
                    three_tv.setTextColor(mResource.getColor(R.color.head_bg));
                    // 切换密码注册界面.
                    register_two_lay.setVisibility(View.GONE);
                    ok_ver_code_btn.setVisibility(View.GONE);
                    register_three_lay.setVisibility(View.VISIBLE);
                    ok_register_btn.setVisibility(View.VISIBLE);
                } else {
                    OpenToast.showMsg("验证码错误");
                }
            }
        });
    }

    /*
     * 注册
     */
    @OnClick(R.id.ok_register_btn)
    void ok_register_btn(View view) {
        register(phoneStr, password);
    }

    @Override
    public void onRegisterSuccess(User user) {
        OpenToast.showMsg("注册成功");
        EventBus.getDefault().post(user); // 更新我的页面信息.
        finish();
    }

    @Override
    public void onRegisterFailure(int code, String msg) {
        OpenToast.showMsg(msg);
    }

    /*
     * 法律条文是否勾选了.
     */
    void articleChkCheckedChange(boolean check) {
        ver_code_btn.setEnabled(check);
    }

    /*
     * 手机号码输入.
     */
    void userEtAfterTextChange(Editable text) {
        // 判断是否为手机号码并且法律条文勾选了!!.
        if (CommonUtils.isMobileNO(text.toString()) && article_chk.isChecked()) {
            ver_code_btn.setEnabled(true);
        } else {
            ver_code_btn.setEnabled(false);
        }
    }

    /**
     * 验证码输入.
     */
    void verCodeEtAfterTextChange(Editable text) {
        // 判断验证码是否不空.
        if (!TextUtils.isEmpty(text.toString())) {
            ok_ver_code_btn.setEnabled(true);
        } else {
            ok_ver_code_btn.setEnabled(false);
        }
    }

    /*
     * 密码输入
     */
    void passEtAfterTextChange(Editable text) {
        password = text.toString();
    }

    /*
     * 密码确认输入
     */
    void passReptEtAfterTextChange(Editable text) {
        String reptPass = text.toString();
        // 判断两次密码是否一致，不然不给注册.(fuck... ...)
        if (password.equals(reptPass)) {
            ok_register_btn.setEnabled(true);
        } else {
            ok_register_btn.setEnabled(false);
        }
    }

}
